# Maintainer: David Runge <dvzrv@archlinux.org>
# Contributor: Sven-Hendrik Haase <sh@lutzhaase.com>
# Contributor: Filip <fila pruda.com>

pkgname=postfixadmin
pkgver=3.3.13
pkgrel=1
pkgdesc="Web based interface to manage Postfix mailboxes, virtual domains and aliases"
arch=(any)
url="https://postfixadmin.sourceforge.io/"
license=(GPL2)
depends=(php)
optdepends=(
  'apache: run in webserver'
  'dovecot: interface local IMAP/POP3 server'
  'mariadb: use local MySQL server'
  'perl: for example scripts'
  'php-fpm: run in fastCGI process manager'
  'php-imap: use IMAP'
  'php-sqlite: use SQLite database backend'
  'php-pgsql: use PostgreSQL database backend'
  'postfix: interface local mail server'
  'postgresql: use local PostgreSQL server'
  'python: for example scripts'
  'uwsgi-plugin-php: run as application container'
)
backup=(
  etc/webapps/postfixadmin/config.inc.php
  etc/webapps/postfixadmin/config.local.php
  etc/webapps/postfixadmin/.htaccess
  etc/uwsgi/postfixadmin.ini
)
source=(
  https://github.com/postfixadmin/$pkgname/archive/$pkgname-$pkgver.tar.gz
  $pkgname.sysusers
  $pkgname.tmpfiles
  $pkgname.uwsgi
)
install=$pkgname.install
sha512sums=('bf7daaa089ee3adc4b557f1a7d0509d78979ef688fb725bab795f5c9d81e8774296245fde0cb184db51e9185cad381682c3ecc0bfadf852388b499a0a95cca64'
            'dc61527dbec79a3b19c8fcbc424fbfbe483cccf99a606e566ea6cd624d46c70a6d87dd8d4da398fedf12b60cfd0cf364c351d6b826e6eee6b2ca8eac0d9efcbe'
            '7e55c844be7b186d9eb568fe097632651c683582da83e0ee24cd6dd7c5a581fe05e5462174237f542ff8dc5d17a8ab651a77f0087a0c8df01d7b36c7722b1440'
            '2e3854d97bd74dc2501260c557549b0162f4a7749261538672de052b252e56fa62ef052bafe6d8681f2f87a94b71039fc9a68ff622a04dd2d6232213143d1840')
b2sums=('0b5160e9996fa3b3580ee4f7dde128d6c4335c9929600158bb4e431f337b3a5ff6fe92cf8657102698b051b10c47ef134eb0a9d13d6675a3c701988bf4cefcb1'
        'a73314d9befe48869ca121bddfb3af61e916f8458f388e13eed1a7ffd64e7ae75604dc3af92d3d4d5b97c30d3cb089e28cbf64c7e81e324f3ff85d75b15b5f0d'
        '0e0170b08284dcfc5a68afd46a2e726d696905f3ddcd8158fed68c84fec039fabe60379005055e5fa1cc1fbb098c107b739d0e107f027bd7054d0e31b6e9cea6'
        'b59885eec24483d0f65614918f7c8cd70402600ac645f14560d5d28dfddce84a41a3ce1e2f9ea4bc0364ce299f468a2a243b0cbe920c0526f3c92e3d0269fa0c')

prepare(){
  cd $pkgname-$pkgname-$pkgver
  # create empty local config (for overrides)
  printf "<?php\n" > config.local.php
  # create .htaccess file
  printf "#Require all denied\n" > .htaccess
  # remove unneeded executable bits
  find . -executable -type f -and -not -path "*scripts*" -exec chmod -c 644 {} \;
  # remove symlinks
  find . -type l -exec rm -v {} +
  # remove unneeded debian directories
  rm -rv ADDITIONS/squirrelmail-plugin/debian
}

package() {
  cd $pkgname-$pkgname-$pkgver

  # sysusers.d integration
  install -vDm 644 ../$pkgname.sysusers "$pkgdir/usr/lib/sysusers.d/$pkgname.conf"
  # tmpfiles.d integration
  install -vDm 644 ../$pkgname.tmpfiles "$pkgdir/usr/lib/tmpfiles.d/$pkgname.conf"
  # docs
  install -vDm 644 {README.md,CHANGELOG.TXT} -t "$pkgdir/usr/share/doc/$pkgname/"
  install -vDm 644 DOCUMENTS/{README*,*.{txt,md}} -t "$pkgdir/usr/share/doc/$pkgname/documents"
  cp -av ADDITIONS VIRTUAL_VACATION "$pkgdir/usr/share/doc/$pkgname"

  # webapp
  find configs languages lib model public scripts templates tests \
    -type f \( \
    -iname "*.conf" -or \
    -iname "*.css" -or \
    -iname "*.eot" -or \
    -iname "*.gif" -or \
    -iname "*.ico" -or \
    -iname "*.js" -or \
    -iname "*.lang" -or \
    -iname "*.lib" -or \
    -iname "*.map" -or \
    -iname "*.php" -or \
    -iname "*.png" -or \
    -iname "*.sql" -or \
    -iname "*.svg" -or \
    -iname "*.tpl" -or \
    -iname "*.ttf" -or \
    -iname "*.woff" -or \
    -iname "*.woff2" \
    \) -exec install -vDm 644 {} "$pkgdir/usr/share/webapps/$pkgname/"{} \;
  install -vDm 644 {common,functions.inc,index}.php phpunit.xml -t "$pkgdir/usr/share/webapps/$pkgname"
  (
    # adding weird symlinks
    cd "$pkgdir/usr/share/webapps/$pkgname/public"
    ln -sv users/password-change.php .
    ln -sv users/password-recover.php .
  )

  # config
  install -vDm 640 config.{inc,local}.php -t "$pkgdir/etc/webapps/$pkgname/"
  ln -sv /etc/webapps/$pkgname/config.inc.php "$pkgdir/usr/share/webapps/$pkgname/config.inc.php"
  ln -sv /etc/webapps/$pkgname/config.local.php "$pkgdir/usr/share/webapps/$pkgname/config.local.php"
  install -vDm 644 .htaccess -t "$pkgdir/etc/webapps/$pkgname/"
  ln -sv /etc/webapps/$pkgname/.htaccess "$pkgdir/usr/share/webapps/$pkgname/.htaccess"
  install -vDm 644 ../$pkgname.uwsgi "$pkgdir/etc/uwsgi/$pkgname.ini"
  # template caches
  install -vdm 755 "$pkgdir/var/cache/$pkgname/templates_c"
  ln -sv /var/cache/$pkgname/templates_c "$pkgdir/usr/share/webapps/$pkgname/"
}
